<?php
namespace app\admin\controller;

use think\Request;
use think\Validate;

use app\admin\server\Menu;
use app\admin\model\apower;

class Node extends AdminBase
{
    /**
     * 节点
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if (Request::instance()->isPost()) {
            try {
                $node = [
                    [
                        'id' => 0,
                        'columnid' => -1,
                        'title' => '顶级',
                        'spread' => true,
                        'isJump' => false,
                        'children' => Menu::getNode(),
                    ]

                ];
                $json = ['status' => 200, 'msg' => 'success', 'data' => $node];
            } catch (\Exception $e) {
                $json = ['status' => 400, 'msg' => $e->getMessage()];
            }
            return json($json);
        }

        return $this->fetch();
    }

    /**
     * 添加/编辑 节点
     * @return mixed
     */
    public function save()
    {
        if (Request::instance()->isPost()) {
            try {
                $post = input();
                $post['controller'] = ucfirst($post['controller']);

                $rule = [
                    ['powerid', 'egt:0', 'powerid不合法'],
                    ['columnid', 'require|egt:0', '缺失所属栏目|所属栏目不合法'],
                    ['powername', 'require|length:1,20', '请填写名称|名称不能超过20个字符'],
                    ['columnid', 'require|egt:0', '缺失所属栏目|所属栏目不合法'],
                    ['controller', 'length:1,50', '方法不能超过50个字符'],
                    ['class', 'length:1,50', '图标不能超过50个字符'],
                    ['type', 'require|in:1,2', '请选择是否是菜单|是否是菜单不合法'],
                    ['status', 'require|in:0,1', '请选择状态|状态不合法'],
                    ['weight', 'require|egt:0', '缺失权重|权重不合法'],
                    ['describe', 'length:1,50', '描述不能超过50个字符'],
                ];
                $validate = new Validate($rule);
                if (!$validate->check($post)) throw new \Exception($validate->getError());

                empty($post['powerid']) ? apower::apowerAdd($post) : apower::apowerUpdate($post);

                Menu::clear();

                $json = ['status' => 200, 'msg' => 'success'];
            } catch (\Exception $e) {
                $json = ['status' => 400, 'msg' => $e->getMessage()];
            }
            return json($json);
        }

        $id = input('id/d');
        if (empty($id)) $this->error('缺少id');

        $type = input('type/s', 'add');

        switch ($type) {
            case 'add':
                $info = [];
                $parentId = $id;
                break;
            case 'update':
                $info = Menu::getOne($id);
                $parentId = $info['columnid'];

                break;
            default:
                $this->error('非法链接');
        }

        $this->assign('id', $id);
        $this->assign('info', $info);
        $this->assign('parentId', $parentId);
        $this->assign('nodeTree', Menu::getNodeTree());

        return $this->fetch();
    }

    /**
     * 删除节点
     * @return \think\response\Json
     */
    public function del()
    {
        try {
            $id = input('id/d');
            if (empty($id)) throw new \Exception('缺少id');

            apower::apowerDel(['powerid' => $id]);
            Menu::clear();

            $json = ['status' => 200, 'msg' => 'success'];
        } catch (\Exception $e) {
            $json = ['status' => 400, 'msg' => $e->getMessage()];
        }
        return json($json);
    }

    /**
     * 图标页
     * @return mixed
     */
    public function icon()
    {
        return $this->fetch();
    }
}